package com.ruoyi.releation.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.releation.domain.People;
import com.ruoyi.releation.domain.Releation;
import com.ruoyi.releation.service.IPeopleService;

/**
 * 人员关系
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/releation")
public class ReleationController extends BaseController
{
    private String prefix = "releation";
    
    @Autowired
    private IPeopleService peopleService;

    @GetMapping("/{id}")
    public String people(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("people", peopleService.selectPeopleById(id));
        return prefix + "/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Releation releation)
    {
        startPage();
        List<Releation> list = peopleService.selectReleationList(releation);
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("peoples", peopleService.selectPeopleList(new People()));
        return prefix + "/add";
    }
    
    @GetMapping("/add/{id}")
    public String add2(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("people", peopleService.selectPeopleById(id));
        mmap.put("peoples", peopleService.selectPeopleList(new People()));
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:people:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Releation releation)
    {
        ;
        
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(peopleService.checkReleationUnique(releation)))
        {
            return error("新增人员关系失败，关系已存在");
        }
        
        return toAjax(peopleService.addReleation(releation));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("releation", peopleService.selectReleationById(id));
        mmap.put("peoples", peopleService.selectPeopleList(new People()));
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Releation releation)
    {
        return toAjax(peopleService.updateReleation(releation));
    }

    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(peopleService.deleteReleations(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    
    /**
     * 校验用户名
     */
    @PostMapping("/checkUnique")
    @ResponseBody
    public String checkReleationUnique(Releation releation)
    {
        return peopleService.checkReleationUnique(releation);
    }
}